package com.config.quartz;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfiguration {

    // 配置定时任务1
//    @Bean(name = "firstJobDetail")
    public MethodInvokingJobDetailFactoryBean firstJobDetail(FirstJob firstJob) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(false);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(firstJob);
        // 需要执行的方法
        jobDetail.setTargetMethod("task");
        return jobDetail;
    }

    // 配置触发器1
//    @Bean(name = "firstTrigger")
    public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(firstJobDetail);
        // 设置任务启动延迟
        trigger.setStartDelay(0);
        // 每5秒执行一次
        trigger.setRepeatInterval(5000);
        return trigger;
    }

    // 配置定时任务2
//    @Bean(name = "secondJobDetail")
    public MethodInvokingJobDetailFactoryBean secondJobDetail(SecondJob secondJob) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(false);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(secondJob);
        // 需要执行的方法
        jobDetail.setTargetMethod("task");
        return jobDetail;
    }

    // 配置触发器2
//    @Bean(name = "secondTrigger")
    public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(secondJobDetail);
        // cron表达式
        trigger.setCronExpression("0 30 20 * * ?");
        return trigger;
    }
    
    /**
     * @author   大连寰微科技有限公司.ly
     * @description  paymentStatusJobDetail:配置支付状态更新任务
     * @param paymentStatusJob
     * @return jobDetail
     */
    @Bean(name="paymentStatusJobDetail")
    public MethodInvokingJobDetailFactoryBean paymentStatusJobDetail(PaymentStatusJob paymentStatusJob) {
    	MethodInvokingJobDetailFactoryBean jobDetail =new MethodInvokingJobDetailFactoryBean();
    	// 是否并发执行
        jobDetail.setConcurrent(false);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(paymentStatusJob);
        // 需要执行的方法
        jobDetail.setTargetMethod("task");
		return jobDetail;
    }
    
    /**
     * @author   大连寰微科技有限公司.ly
     * @description  paymentStatusTrigger:配置支付状态更新触发器
     * @param paymentStatusJobDetail
     * @return trigger
     */
    @Bean(name="paymentStatusTrigger")
    public CronTriggerFactoryBean paymentStatusTrigger(JobDetail paymentStatusJobDetail){
    	 CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
         trigger.setJobDetail(paymentStatusJobDetail);
         // cron表达式
         trigger.setCronExpression("0 0 0 * * ?");
         return trigger;
    }

    // 配置Scheduler
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger paymentStatusTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 延时启动，应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器
        bean.setTriggers(paymentStatusTrigger);
        return bean;
    }
}